$(document).ready(function(){
	var contextPath = document.location.pathname;
	var index = contextPath.substr(1).indexOf("/");
	contextPath = contextPath.substr(1, index);
	var basePath = "/" + contextPath;
	var radios = document.getElementsByName("isUse");
<<<<<<< HEAD
	var options = {
			dataType:'json',
			
	        success: function (data) {
	        	 $("#addressBookInfor").modal('hide');
	        	 var result = data.addressList;
	        	 dynamicAddress(result);//动态生成地址列表
	        	
	        }
	    };
=======
>>>>>>> refs/remotes/origin/master
	
	/* var topPosition = 295;
	 var leftPosition = 423;
	
	 var curmarker = $("div[class='curmarker']");
	 for(i=0;i<curmarker.length;i++){
		 var temp = $(curmarker[i]).attr("id");
		 $("#"+temp).css("top",topPosition+"px");
		 $("#"+temp).css("left",leftPosition+"px");
		 if(i%4==0){
			 alert("ksjdfkjsakf"+topPosition);
			 topPosition = topPosition+30;
			 leftPosition = leftPosition-250*4;
		 }
		 leftPosition =leftPosition +250;
	 }*/
	/*for (var i = 0; i < radios.length; i++) {
        radios[i].indexs = i + 1;
        radios[i].onchange = function () {
            if (this.checked) {
            	
            	var addressId = $(this).attr("addressId");
            	alert("addressId-------------->"+addressId);
            	var queryUrl =basePath+"/HRTWEB/address/getAddress.do?id="+addressId;
            	$.ajax({
					url : queryUrl,
					dataType : 'json',
					cache : false,
					success : function(data) {
						var address = data.addressBook;
						alert(address.province);
						alert(address.postCode);
						alert(address.receiver);
						$("#receiver").attr("value",address.receiver);
						$("#postCode").attr("value",address.postCode);
						$("#province").attr("value",address.province);
					},
					error : function(html) {
						alert("出错了");
						return;
					}
				});
            }
        }
    }*/
	/**
	 * 添加事件
	 */
	$("#publicMain").delegate('#addBtn', 'click', function() {
		initProvince();//加载省份
		
		$("#addressId").val("");
		$("#temp").val("");
		$("#address").val("");
		$("#postCode").val("");
		$("#receiver").val("");
		$("#mobile").val("");
		$("#telPhone").val("");
		
		
		$("#addressBookInfor").modal('show');
	});
	/**
	 * 修改按钮事件
	 */

	$("#publicMain")
			.delegate(
					'#editBtn',
					'click',
					function() {
						var memberId = $(this).attr("memberId");
						var addressId = $(this).attr("addressId");
						var editUrl = basePath+"/HRTWEB/address/edit.do";
						$.ajax({

							url : editUrl,
							type : "POST",
							dataType : 'json',
							data : {
								memberId : memberId,
								addressId:addressId
							},
							cache : false,

							success : function(data) {
								var address = data.address;
								loadAddress(address);
							},
							error : function(html) {
								alert("Load Data failly");
								refresh();
							}
						});

					});
	/**
	 * 设置默认地址
	 */
	$("#publicMain")
	.delegate(
			'#setDefaultBtn',
			'click',
			function() {
				var memberId = $(this).attr("memberId");
				var addressId = $(this).attr("addressId");
				alert("memberId---------"+memberId);
				alert("addressId---------"+addressId);
				var editUrl = basePath+"/HRTWEB/address/setDefaultAddress.do";
				$.ajax({
					
					url : editUrl,
					type : "POST",
					dataType : 'json',
					data : {
						memberId : memberId,
						addressId:addressId
					},
					cache : false,
					
					success : function(data) {
						var address = data.address;//返回默认地址
						alert(address.province);
						alert(address.postCode);
						alert(address.receiver);
						$("#receiver").attr("value",address.receiver);
						$("#postCode").attr("value",address.postCode);
						$("#province").attr("value",address.province);
						var result = data.addressList;//返回地址列表
						dynamicAddress(result);
					},
					error : function(html) {
						alert("Load Data failly");
						refresh();
					}
				});
				
			});
	
	/**
	 * 模态窗口关闭时调用
	 */
	$("#addressBookInfor").on('hide.bs.modal', function() {
		clearAddress();
	});
	
	/**
	 * 地址
	 */
	 $(".inner").delegate(
			  "div[class='innerDIV']",
			  "click",
			  function(){
				 /* //选择标志的默认位置
				  var topPosition = 295px;
				  var leftPosition = 423px;*/
				  
				 var addressDIV =  $(this).attr("id");
				 var addressId =  $(this).attr("objId");
				 
				 var queryUrl =basePath+"/HRTWEB/address/getAddress.do?id="+addressId;
	            	$.ajax({
						url : queryUrl,
						dataType : 'json',
						cache : false,
						success : function(data) {
							var address = data.addressBook;
							alert(address.province);
							alert(address.postCode);
							alert(address.receiver);
							$("#receiver").attr("value",address.receiver);
							$("#postCode").attr("value",address.postCode);
							$("#province").attr("value",address.province);
							var tempAddress = address.province + address.city +address.address;
							$("#currentAddress").text(address.tempAddress);
							$("#currentReceiver").text(address.receiver);
						},
						error : function(html) {
							alert("出错了");
							return;
						}
					});
					
				 $("#"+addressDIV).attr("isselected","selected");
				 $("#"+addressDIV).css("border","1px red solid");
				 
				 var divTotal = $("div[class='innerDIV']");
				 
				 var divObject = $("div[isselected='false']");
				 
				
				 
				 for(i=0;i<divObject.length;i++){
					var temp = $(divObject[i]).attr("id");
					$("#"+temp).css("border","");
				 }
				 
				 for(i=0;i<divTotal.length;i++){
						var temp = $(divTotal[i]).attr("id");
						$("#"+temp).attr("isselected","false");
						
				 }
				 
					/*$(".curmarker1").css("visibility","visible");
					$(".curmarker2").css("visibility","hidden");*/
			  });
			  
			   
	 $("#saveBtn").click(function () {
		 $("#addressInfor").attr("method", "post");

			var addressId = $("#addressId").val();
			alert("addressId---------->"+addressId);

			if (addressId == "") {
				$("#addressInfor").attr("action", ""+basePath+"/HRTWEB/address/save.do");
			} else {

				$("#addressInfor").attr("action", ""+basePath+"/HRTWEB/address/update.do");
			}
         $("#addressInfor").ajaxSubmit(options);
        
     });
	 
	 function initProvince(){
		 var seachprov = $("#seachprov");
			var editUrl = basePath+"/HRTWEB/address/province/list.do";
			$.ajax({

				url : editUrl,
				dataType : 'json',
				cache : false,

				success : function(data) {
					var results = data.provinceList;
					
					var rowContent = "<option  value ='-1' >请选择</option>";
					if(results!=null){
						
						for(i=0;i<results.length;i++){
							var province =results[i];
							rowContent = rowContent+"<option  value ="+province.code+" >"+province.name+"</option>";
						}
					}
					seachprov.append(rowContent);
				},
				error : function(html) {
					alert("Load Data failly");
					refresh();
				}
			});
	 }
});


function loadAddress(address) {
	$("#memberId").val(address.memberId);
	$("#addressId").val(address.id);
	$("#address").val(address.address);
	$("#postCode").val(address.postCode);
	$("#receiver").val(address.receiver);
	$("#mobile").val(address.mobile);
	$("#telPhone").val(address.telPhone);
	$("#addressBookInfor").modal('show');
}
function clearTable() {
	$("#addressBookTable tr:not(:first)").remove();
}
function clearAddress() {
	$("#seachprov").empty(); 
	$("#seachcity").empty(); 
	$("#seachdistrict").empty(); 
}
function refresh() {
	window.location.reload();
}

function dynamicAddress(result){
	 clearTable();
	 var addressBookTable = $("#addressBookTable");
	
	 
	/* var rowContent ="";
	 for(i=0;i<result.length;i++){
		 var address = result[i];
		 rowContent = rowContent+"<tr><td><input type='radio' id='isUse'  name='isUse' addressId='"+address.id+"'/><span id='province'>"+address.province+"</span><div><button addressId='"+address.id+"' memberId='"+address.memberId+"' id='setDefaultBtn'>设为默认</button></div></td>"
         +"<td class='buyGift'><button type='button' id='editBtn' data-toggle='modal' memberId='"+address.memberId+"' addressId='"+address.id+"'>修改本地址</button></td>"
         +"</tr>";
	 }
	 rowContent = rowContent+"<tr><td class='buyGift' colspan='2'>"
	+"<button type='button' id='addBtn' data-toggle='modal' memberId='${hrtMember.id}'>添加新地址</button></td></tr>";*/
	 
	 var rowContent = "<tr><td colspan='2'><div class='inner'>";
	 for(i=0;i<result.length;i++){
		 var address = result[i];
		 rowContent = rowContent+"<ul><li style='padding-top:15px;'>";
		 if(address.isdefault==0){
			 
			 rowContent = rowContent+"<div class='innerDIV' id='address'"+address.id+"'' isselected='false' objId='"+address.id+"'>";
		 }
		 else{
			 rowContent = rowContent+"<div class='innerDIV' id='address'"+address.id+"'' isselected='false' objId='"+address.id+"' style='border:1px red solid;'>";
			 
		 }
		 rowContent = rowContent+"<div><span>"+address.province+"</span><span>"+address.city+"</span><span>(</span><span>"+address.receiver+"收</span><span>)</span></div>";
		 rowContent = rowContent+"<div><span>天河区</span><span>"+address.address+"</span><span>"+address.mobile+"</span></div>";
		 rowContent = rowContent+ "<a href='javascript::;' id='editBtn' data-toggle='modal' memberId='"+address.memberId+"' addressId='"+address.id+"'>修改</a><a href='javascript::' addressId='"+address.id+"' memberId='"+address.memberId+"' id='setDefaultBtn'>设为默认</a></div></li></ul>";
		 
		 
	 }
	 rowContent = rowContent+"</div></td></tr>";
	 addressBookTable.append(rowContent);
}



